package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.Cat.CatActivity;
import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.Garbagesorting.GarbageActivity;
import com.example.myapplication.Outpatientappointment.OutpatientActivity;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LogisticsclassificationBean;
import com.example.myapplication.ShuJu.LogisticsinquircarouselmapBean;
import com.example.myapplication.ShuJu.Re_Servlet;
import com.example.myapplication.government.GovernmentMainActivity;
import com.example.myapplication.library.LibraryMainActivity;
import com.example.myapplication.logisticsinquiry.LogisticscompanyActivity;
import com.example.myapplication.urbansubway.UrbansubwayActivity;
import com.example.myapplication.volunteerservice.Volunteer;

import java.util.List;

import smartbus.SmartbusActivity;

public class ServerApter extends  RecyclerView.Adapter<ServerApter.MyServer>{
    private Context context ;
    private List<Re_Servlet.RowsDTO> list;
    private View view;
    private String  BeanName;
    private List<LogisticsclassificationBean.DataBean> dataBeans;



    public ServerApter(Context context, List<Re_Servlet.RowsDTO> list) {
        this.context = context;
        this.list = list;

    }

    public ServerApter(Context context , List<LogisticsclassificationBean.DataBean> dataBeans, String beanName) {
        this.context = context;
        this.BeanName = beanName;
        this.dataBeans = dataBeans;
    }

        @NonNull
    @Override
    public MyServer onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充试图
        view = LayoutInflater.from(context).inflate(R.layout.item_sever,null);
        return new MyServer(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyServer holder, int position) {

        if(BeanName==null){
            String imgUrl =TokenBean.IP+list.get(position).getImgUrl();
            if(position !=9 ){
                Glide.with(context).load(imgUrl).into(holder.sever_tv_img);
                holder.sever_iv_text.setText(list.get(position).getServiceName());
                //点击进入数字图书馆
                if(list.get(position).getId()==30){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, LibraryMainActivity.class);
                            context.startActivity(intent);
                        }
                    });
                }

                //点击进入政府热线
                if(list.get(position).getId()==29){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, GovernmentMainActivity.class);
                            context.startActivity(intent);
                        }
                    });
                }

                //点击进入停哪儿
                if(list.get(position).getId()==17){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, CatActivity.class);
                            context.startActivity(intent);
                        }
                    });
                }

                //点击进入门诊预约
                if(list.get(position).getId()==5){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, OutpatientActivity.class);
                            context.startActivity(intent);
                        }
                    });
                }

                //点击进入垃圾分类
                if(list.get(position).getId()==31){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, GarbageActivity.class);
                            context.startActivity(intent);
                        }
                    });
                }


                //智慧巴士
                if(list.get(position).getId()==3){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, SmartbusActivity.class);
                            context.startActivity(intent);
                        }
                    });
                }

                //志愿服务
                if(list.get(position).getId()==32){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, Volunteer.class);
                            context.startActivity(intent);
                        }
                    });
                }



                //城市地铁
                if(list.get(position).getId()==2){
                    holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, UrbansubwayActivity.class);
                            context.startActivity(intent);
                        }
                    });
                }

            }else {
                Glide.with(context).load(imgUrl).into(holder.sever_tv_img);
                holder.sever_iv_text.setText("更多服务");
                holder.sever_iv_text.setTextSize(14);
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.v("fwling","holder.sever_tv_img-click");
                        Intent intent = new Intent("botviewReceiver_action");
                        context.sendBroadcast(intent);
                    }
                });
            }
        }else{
            Glide.with(context).load(TokenBean.IP+dataBeans.get(position).getImgUrl()).into(holder.sever_tv_img);
            holder.sever_iv_text.setText(dataBeans.get(position).getName());
            holder.li_sever.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LogisticscompanyActivity.class);
                    intent.putExtra(Commom.LogisticscompanID,String.valueOf(dataBeans.get(position).getId()));
                    context.startActivity(intent);
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        if(BeanName==null){
            return 10;
        }else {
            return 12;
        }

    }

    class  MyServer extends RecyclerView.ViewHolder{
        private final RelativeLayout li_sever;
        private final TextView sever_iv_text;
        private final ImageView sever_tv_img;

        public MyServer(@NonNull View itemView) {
            super(itemView);
            li_sever = itemView.findViewById(R.id.li_sever);
            sever_iv_text = itemView.findViewById(R.id.sever_iv_text);
            sever_tv_img = itemView.findViewById(R.id.sever_tv_img);
        }
    }
}
